package org.parademo.model;

import java.util.*;

/**
 * @author Ajit Skanda
 * @version 1.0
 */
public class MemberEntity extends Entity {

    private String memberName;
    private String password;
    private String firstName;
    private String lastName;
    private Date joinDate;
    private Date birthDate;
	private CommunityEntity community;
	private Map<Category, List<TokenCreditEntity>> tokens;
	private List<ProposalEntity> myProposals;

    public MemberEntity(String pName, String pPassword, String pFirstName, String pLastName, Date pJoinDate,
        Date pBirthDate, CommunityEntity pCommunity) {
	    id = new Random().nextLong();
		memberName = pName;
		password = pPassword;
		firstName = pFirstName;
		lastName = pLastName;
		joinDate = pJoinDate;
		birthDate = pBirthDate;
		community = pCommunity;

	    if (community != null) {
		    community.getMembers().add(id);
	    }

	    myProposals = new ArrayList<ProposalEntity>();

	    tokens = new HashMap<Category, List<TokenCreditEntity>>();
	    tokens.put(Category.Internal, new ArrayList<TokenCreditEntity>());
	    tokens.put(Category.Defence, new ArrayList<TokenCreditEntity>());
	    tokens.put(Category.Economy, new ArrayList<TokenCreditEntity>());
	    tokens.put(Category.Education, new ArrayList<TokenCreditEntity>());
	    tokens.put(Category.Environment, new ArrayList<TokenCreditEntity>());
	    tokens.put(Category.Philosophy, new ArrayList<TokenCreditEntity>());
	    tokens.put(Category.Politics, new ArrayList<TokenCreditEntity>());
	    tokens.put(Category.Sports, new ArrayList<TokenCreditEntity>());
    }

    public String getMemberName() {
        return memberName;
    }

    public void setMemberName(String pMemberName) {
        String oldMemberName = memberName;
	    memberName = pMemberName;
	    pcs.firePropertyChange("memberName", oldMemberName, pMemberName);
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String pPassword) {
	    String oldPassword = password;
        password = pPassword;
	    pcs.firePropertyChange("password", oldPassword, pPassword);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String pFirstName) {
	    String oldFirstName = firstName;
        firstName = pFirstName;
	    pcs.firePropertyChange("firstName", oldFirstName, pFirstName);
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String pLastName) {
	    String oldLastName = lastName;
        lastName = pLastName;
	    pcs.firePropertyChange("lastName", oldLastName, pLastName);
    }

    public Date getJoinDate() {
        return joinDate;
    }

    public void setJoinDate(Date pJoinDate) {
	    Date oldJoinDate = joinDate;
        joinDate = pJoinDate;
	    pcs.firePropertyChange("joinDate", oldJoinDate, pJoinDate);
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date pBirthDate) {
		Date oldBirthDate = birthDate;
        birthDate = pBirthDate;
	    pcs.firePropertyChange("birthDate", oldBirthDate, pBirthDate);
    }

    @Override
    public String toString() {
        return "MemberEntity{" +
                "id='" + id + '\'' +
                ", memberName='" + memberName + '\'' +
                ", password='" + password + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", joinDate=" + joinDate +
                ", birthDate=" + birthDate +
                '}';
    }

	public List<ProposalEntity> getMyProposals() {
		return myProposals;
	}

	public void setMyProposals(List<ProposalEntity> pMyProposals) {
		List<ProposalEntity> oldProposals = myProposals;
		myProposals = pMyProposals;
		pcs.firePropertyChange("myProposals", oldProposals, pMyProposals);
	}

	public CommunityEntity getCommunity() {
		return community;
	}

	public void setCommunity(CommunityEntity pCommunity) {
		CommunityEntity oldCommunity = community;
		community = pCommunity;
		pcs.firePropertyChange("community", oldCommunity, pCommunity);
	}

	public Map<Category, List<TokenCreditEntity>> getTokens() {
		return tokens;
	}

	public void setTokens(Map<Category, List<TokenCreditEntity>> tokens) {
		this.tokens = tokens;
	}

	/**
	 * Set tokens for a given category.
	 * @param category to which the tokens belong.
	 * @param categoryTokens the List of tokens.
	 */
	public void setTokensByCategory(Category category, List<TokenCreditEntity> categoryTokens) {
		this.tokens.remove(category);
		this.tokens.put(category, categoryTokens);
	}

	/**
	 * Get a List of {@link TokenCreditEntity} objects, i.e, tokens for a given category for the member.
	 * @param category for which the tokens are fetched.
	 * @return List of tokens for the given category.
	 */
	public List<TokenCreditEntity> getTokensByCategory(Category category) {
		return this.tokens.get(category);
	}
}